<template>
  <div class="connection-test">
    <div class="test-header">
      <h1>前后端连接测试</h1>
      <p>验证前端与后端API的连接状态</p>
    </div>

    <div class="test-sections">
      <!-- 基础API测试 -->
      <div class="test-section">
        <h3>基础API测试</h3>
        <div class="test-buttons">
          <el-button @click="testBasicAPI" :loading="loading.basic">测试基础API</el-button>
          <el-button @click="testHealthAPI" :loading="loading.health">健康检查</el-button>
          <el-button @click="testInfoAPI" :loading="loading.info">服务器信息</el-button>
        </div>
        <div v-if="results.basic" class="test-result">
          <h4>基础API结果:</h4>
          <pre>{{ JSON.stringify(results.basic, null, 2) }}</pre>
        </div>
      </div>

      <!-- 视频API测试 -->
      <div class="test-section">
        <h3>视频API测试</h3>
        <div class="test-buttons">
          <el-button @click="testVideosAPI" :loading="loading.videos">获取视频列表</el-button>
          <el-button @click="testBannersAPI" :loading="loading.banners">获取轮播图</el-button>
          <el-button @click="testRecommendedAPI" :loading="loading.recommended">获取推荐视频</el-button>
        </div>
        <div v-if="results.videos" class="test-result">
          <h4>视频API结果:</h4>
          <pre>{{ JSON.stringify(results.videos, null, 2) }}</pre>
        </div>
      </div>

      <!-- 管理员API测试 -->
      <div class="test-section">
        <h3>管理员API测试</h3>
        <div class="test-buttons">
          <el-button @click="testAdminStatsAPI" :loading="loading.adminStats">获取统计数据</el-button>
          <el-button @click="testAdminVideosAPI" :loading="loading.adminVideos">获取视频管理数据</el-button>
          <el-button @click="testAdminCategoriesAPI" :loading="loading.adminCategories">获取分类数据</el-button>
        </div>
        <div v-if="results.adminStats" class="test-result">
          <h4>管理员API结果:</h4>
          <pre>{{ JSON.stringify(results.adminStats, null, 2) }}</pre>
        </div>
      </div>

      <!-- 数据库连接测试 -->
      <div class="test-section">
        <h3>数据库连接测试</h3>
        <div class="test-buttons">
          <el-button @click="testDatabaseConnection" :loading="loading.database">测试数据库连接</el-button>
        </div>
        <div v-if="results.database" class="test-result">
          <h4>数据库连接结果:</h4>
          <pre>{{ JSON.stringify(results.database, null, 2) }}</pre>
        </div>
      </div>

      <!-- 连接状态 -->
      <div class="connection-status">
        <h3>连接状态</h3>
        <div class="status-grid">
          <div class="status-item" :class="{ success: status.frontend, error: !status.frontend }">
            <el-icon><Monitor /></el-icon>
            <span>前端服务</span>
            <span>{{ status.frontend ? '运行中' : '未运行' }}</span>
          </div>
          <div class="status-item" :class="{ success: status.backend, error: !status.backend }">
            <el-icon><Server /></el-icon>
            <span>后端服务</span>
            <span>{{ status.backend ? '运行中' : '未运行' }}</span>
          </div>
          <div class="status-item" :class="{ success: status.database, error: !status.database }">
            <el-icon><Database /></el-icon>
            <span>数据库</span>
            <span>{{ status.database ? '已连接' : '未连接' }}</span>
          </div>
          <div class="status-item" :class="{ success: status.api, error: !status.api }">
            <el-icon><Link /></el-icon>
            <span>API连接</span>
            <span>{{ status.api ? '正常' : '异常' }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElButton, ElIcon } from 'element-plus'
import { Monitor, Server, Database, Link } from '@element-plus/icons-vue'
import api from '@/utils/axios'

// 响应式数据
const loading = reactive({
  basic: false,
  health: false,
  info: false,
  videos: false,
  banners: false,
  recommended: false,
  adminStats: false,
  adminVideos: false,
  adminCategories: false,
  database: false
})

const results = reactive({
  basic: null,
  health: null,
  info: null,
  videos: null,
  banners: null,
  recommended: null,
  adminStats: null,
  adminVideos: null,
  adminCategories: null,
  database: null
})

const status = reactive({
  frontend: true,
  backend: false,
  database: false,
  api: false
})

// 测试函数
const testBasicAPI = async () => {
  loading.basic = true
  try {
    const response = await api.get('/test')
    results.basic = response
    status.api = true
  } catch (error) {
    results.basic = { error: error.message }
    status.api = false
  } finally {
    loading.basic = false
  }
}

const testHealthAPI = async () => {
  loading.health = true
  try {
    const response = await api.get('/health')
    results.health = response
    status.backend = true
  } catch (error) {
    results.health = { error: error.message }
    status.backend = false
  } finally {
    loading.health = false
  }
}

const testInfoAPI = async () => {
  loading.info = true
  try {
    const response = await api.get('/info')
    results.info = response
  } catch (error) {
    results.info = { error: error.message }
  } finally {
    loading.info = false
  }
}

const testVideosAPI = async () => {
  loading.videos = true
  try {
    const response = await api.get('/videos')
    results.videos = response
  } catch (error) {
    results.videos = { error: error.message }
  } finally {
    loading.videos = false
  }
}

const testBannersAPI = async () => {
  loading.banners = true
  try {
    const response = await api.get('/videos/banners')
    results.banners = response
  } catch (error) {
    results.banners = { error: error.message }
  } finally {
    loading.banners = false
  }
}

const testRecommendedAPI = async () => {
  loading.recommended = true
  try {
    const response = await api.get('/videos/recommended')
    results.recommended = response
  } catch (error) {
    results.recommended = { error: error.message }
  } finally {
    loading.recommended = false
  }
}

const testAdminStatsAPI = async () => {
  loading.adminStats = true
  try {
    const response = await api.get('/admin/stats')
    results.adminStats = response
  } catch (error) {
    results.adminStats = { error: error.message }
  } finally {
    loading.adminStats = false
  }
}

const testAdminVideosAPI = async () => {
  loading.adminVideos = true
  try {
    const response = await api.get('/admin/videos')
    results.adminVideos = response
  } catch (error) {
    results.adminVideos = { error: error.message }
  } finally {
    loading.adminVideos = false
  }
}

const testAdminCategoriesAPI = async () => {
  loading.adminCategories = true
  try {
    const response = await api.get('/admin/categories')
    results.adminCategories = response
  } catch (error) {
    results.adminCategories = { error: error.message }
  } finally {
    loading.adminCategories = false
  }
}

const testDatabaseConnection = async () => {
  loading.database = true
  try {
    const response = await api.get('/admin/stats')
    if (response.success) {
      results.database = {
        success: true,
        message: '数据库连接正常',
        data: response.data
      }
      status.database = true
    }
  } catch (error) {
    results.database = { 
      success: false, 
      error: error.message,
      message: '数据库连接失败'
    }
    status.database = false
  } finally {
    loading.database = false
  }
}

onMounted(() => {
  // 自动测试基础连接
  testBasicAPI()
  testHealthAPI()
})
</script>

<style scoped>
.connection-test {
  min-height: 100vh;
  background: #f9fafb;
  padding: 40px 20px;
}

.test-header {
  text-align: center;
  margin-bottom: 40px;
}

.test-header h1 {
  font-size: 2.5rem;
  color: #1f2937;
  margin-bottom: 10px;
}

.test-header p {
  font-size: 1.1rem;
  color: #6b7280;
}

.test-sections {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.test-section {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.test-section h3 {
  font-size: 1.5rem;
  color: #1f2937;
  margin-bottom: 20px;
}

.test-buttons {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.test-result {
  background: #f8fafc;
  border-radius: 8px;
  padding: 20px;
  border: 1px solid #e5e7eb;
}

.test-result h4 {
  margin-bottom: 10px;
  color: #1f2937;
}

.test-result pre {
  background: #1f2937;
  color: #f9fafb;
  padding: 15px;
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.9rem;
  line-height: 1.4;
}

.connection-status {
  background: white;
  border-radius: 12px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.connection-status h3 {
  font-size: 1.5rem;
  color: #1f2937;
  margin-bottom: 20px;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px;
  border-radius: 8px;
  border: 2px solid;
  transition: all 0.3s ease;
}

.status-item.success {
  background: #f0fdf4;
  border-color: #22c55e;
  color: #166534;
}

.status-item.error {
  background: #fef2f2;
  border-color: #ef4444;
  color: #991b1b;
}

.status-item .el-icon {
  font-size: 24px;
}

.status-item span:last-child {
  font-weight: 600;
  margin-left: auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .test-buttons {
    flex-direction: column;
  }
  
  .status-grid {
    grid-template-columns: 1fr;
  }
  
  .test-result pre {
    font-size: 0.8rem;
  }
}
</style>
